package img

import (
	"common/mildware"
	"common/models"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"project/docModel"
)

// SearchImgByAuthorId 搜索图片
// @Summary 搜索图片
// @Description 搜索图片
// @Tags 图片
// @Accept application/json
// @Produce application/json
// @Param Authorization header string true "Bearer 用户令牌"
// @Param object body docModel.DocSearchImgByAuthorId false "搜索参数"
// @Security ApiKeyAuth
// @Success 200 {object} docModel.DocResp
// @Router  /img/search [post]
func (h IHandler) SearchImgByAuthorId(c *gin.Context) {
	var (
		authorId string
		err      error
		res      []*docModel.ImageInfoResp
	)
	resp := new(models.RespData)
	page, size, err := models.GetPageInfo(c)
	if err != nil {
		zap.L().Error("Img :models.GetPageInfo error", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	//获得uid
	authorId, _, _, err = mildware.GetCyrrentUserID(c)
	if err != nil {
		resp.SendCode(c, models.CodeInvalidToken)
		return
	}
	list, total, err := h.M.ImgInfoQueryByAuthId(authorId, page, size)
	if err != nil {
		zap.L().Error("Img :h.M.ImgInfoQueryByAuthId error", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	for _, info := range list {
		demo := &docModel.ImageInfoResp{
			Info:      info,
			ImageHttp: models.GetHttpUrl(info.ImageId, models.HttpImg),
		}
		res = append(res, demo)
	}
	resp.Count = total
	resp.Data = res
	resp.SendSuccess(c)
}
